package au.com.handsunited.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import au.com.handsunited.dao.UserDAO;
import au.com.handsunited.model.User;
import au.com.handsunited.service.SecurityService;
import au.com.handsunited.utils.Encrypter;

/**
 * @author arunda
 * Date: 25/12/2006
 * Time: 10:43:17
 */
public class SecurityServiceImpl implements SecurityService {

	private static Logger logger = LoggerFactory.getLogger(SecurityServiceImpl.class);

	private final UserDAO userDAO;


	/**
	 *
	 * @param userDAO
	 */
	public SecurityServiceImpl(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	/**
	 *
	 * @param username
	 * @param password
	 * @return
	 */
	public User login(String username, String password) {

		User user = userDAO.findUserByUsername(username);

		if (user != null) {
			if (user.getPassword().equals(Encrypter.sha(password))) {
				if (logger.isDebugEnabled()) {
					logger.debug("User [{}] logged in successfully.", username);
				}
				return user;
			} else {
				if (logger.isDebugEnabled()) {
					logger.debug("User [{}] login unsuccessful.", username);
				}
				return null;
			}
		} else {
			if (logger.isDebugEnabled()) {
				logger.debug("User[{}] tried to login but has not registered.", username);
			}
			return null;
		}
	}


	/**
	 *
	 * @param user
	 */
	public void logout(User user) {
//		userDAO.logout(user);
	}

}
